package com.gxa.community.admin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxa.community.admin.mapper.OperateKillSpellMapper;
import com.gxa.community.admin.service.OperateKillSpellService;
import com.gxa.community.core.pojo.domain.OperateKillSpell;
import com.gxa.community.core.pojo.dto.SeckillQueryDto;
import com.gxa.community.core.pojo.vo.EditkillVo;
import com.gxa.community.core.pojo.vo.SeckillAddVo;
import com.gxa.community.core.utils.CodingUtils;
import com.gxa.community.core.utils.R;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 秒杀信息表 前端控制器
 * </p>
 *
 * @author XYJ
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/seckill")
public class SecKillController {
    @Autowired
    private OperateKillSpellService secKillService;
    @Autowired
    private OperateKillSpellMapper operateKillSpellMapper;

    @GetMapping("/seckills")
    public R QueryAllseckill(){
        R r = secKillService.SelectAllSecKill(1);
        return r;
    }

    @GetMapping("preaddseckill")
    public R preaddSeckill(){
        String s = CodingUtils.RandomNum();
        return R.ok().put("data",s);
    }

    @PostMapping("/addseckill")
    public R addSecKill(@RequestBody SeckillAddVo seckillAddVo){
        R r = secKillService.AddSecKill(seckillAddVo,1);
        return r;
    }

    @DeleteMapping("/delseckill")
    public R delSeckill(@NotNull String id){
        R r = secKillService.delSekill(id);
        return r;
    }

    @PutMapping("/editseckill")
    public R editseckill(@RequestBody  EditkillVo editkillVo){
        R r = secKillService.editSeckill(editkillVo,2);
        return r;
    }

    @PutMapping("/editstatus")
    public R editStatus(@NotNull String killId){
        UpdateWrapper<OperateKillSpell> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("kill_id",killId);
        OperateKillSpell operateKillSpell = new OperateKillSpell();
        operateKillSpell.setStatus(1);
        int update = operateKillSpellMapper.update(operateKillSpell, updateWrapper);
        if (update!=0){
            return R.ok("修改成功");
        }
       return R.error("修改失败");
    }

    @GetMapping("/preedit")
    public R preEdit(@NotNull String killId){
        R r = secKillService.preEditSeckill(killId);
        return r;
    }

    @GetMapping("/querybytext")
    public R queryText(@RequestParam(required = false) String name,
                       @Length(min = -1, max = 2)Integer activeStatus,
                       @Length(min = 0, max = 2)Integer status,
                       @RequestParam(required = false) Long startTime, @RequestParam(required = false)Long endTime){
        R r = secKillService.queryText(name, activeStatus, status, startTime, endTime,1);
        return r;
    }

    /**
     * 根据用户当前时间来获取最近的秒杀活动
     * @return 返回一个所有最近秒杀活动的的商品和信息
     */
    @GetMapping("/queryGoodsOne")
    public SeckillQueryDto queryGoodsOne(){
        return secKillService.querykillGoodsOne();
    }

    /**
     * 获取当天所有的秒杀信息
     * @return 返回所有的秒杀信息包括商品
     */
    @GetMapping("/queryGoodsList")
    public List<SeckillQueryDto>  queryGoodsList(){
            return secKillService.querykillGoodsList();
    }
}


